*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

* Authors: Ranjit Lall & David Vilalta

* Paper: Varieties of Anxieties: Disaggregating Emotion and Voting Behavior in the COVID-19 Era

* Purpose: Cleans and formats dataset used for the electoral analysis in Madrid

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

clear all 

* Insert folder path
global folder ".../Insert folder path"

* Set working directory for data files
cd "${folder}"

* Define local folders
global dta "Final_data/"
global tables "Results/Tables/"
global figures "Results/Figures/"
global interm_files "Raw_data/Intermediate_files/Madrid_Election/"
global madrid_files "Raw_data/Madrid_Election/"


use ${madrid_files}cumulative_cases_Mad_ELECTION_map, clear


keep if PROV==28

save ${madrid_files}cumulative_cases_Mad_ELECTION, replace


use ${madrid_files}population, clear

keep if PROV==28


save ${interm_files}mad_population, replace

gen year=2021

import excel "${madrid_files}/MAD_2021_Elections.xls", sheet("Formatted") firstrow clear

save ${interm_files}mad_elections_2021, replace

import excel "${madrid_files}/MAD_2019_Elections.xls", sheet("Formatted") firstrow clear

save ${interm_files}mad_elections_2019, replace

import excel "${madrid_files}/MAD_2015_Elections.xls", sheet("Formatted") firstrow clear

save ${interm_files}mad_elections_2015, replace

import excel "${madrid_files}/MAD_2011_Elections.xls", sheet("Formatted") firstrow clear

save ${interm_files}mad_elections_2011, replace

import excel "${madrid_files}/MAD_2007_Elections.xls", sheet("Formatted") firstrow clear

save ${interm_files}mad_elections_2007, replace


use ${interm_files}cumulative_cases_Mad_ELECTION, clear

merge 1:1 PROV MUN using ${interm_files}mad_population
drop _merge
merge 1:1 MUN using ${interm_files}mad_elections_2021
drop _merge
append using ${interm_files}mad_elections_2019
append using ${interm_files}mad_elections_2015
append using ${interm_files}mad_elections_2011
append using ${interm_files}mad_elections_2007

drop datestring date PROVINCIA HOMBRES MUJERES NOMBRE

gen cumcases=cumulative_cases/POB20

replace cumcases=0 if cumcases==.

rename Proconfin proconfin
rename Anticonfin anticonfin

bys MUN: egen covid= max(cumcases>0.0816 & year==2021)

gen incidence=1
replace incidence=2 if cumcases>0.0659
replace incidence=3 if cumcases>0.0816
replace incidence=4 if cumcases>0.0954

forval i=1/4{
    bys MUN: egen t2021_`i'= max(incidence==`i' & year==2021)
}

keep proconfin anticonfin year t2021_1 t2021_2 t2021_3 t2021_4 covid

label var covid "Above/Below Median Covid Levels"
label var proconfin "Vote Share of Pro-Lockdown Parties"
label var anticonfin "Vote Share of Anti-Lockdown Parties"
label var year "Year"
label var t2021_1 "First Quartile"
label var t2021_2 "Second Quartile"
label var t2021_3 "Third Quartile"
label var t2021_4 "Fourth Quartile"


save ${dta}Finaldata_Madrid_Commontrends, replace
